package zjf.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;




public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware,Serializable{

	protected static Log log = null;
	public Map<String, String> parms;//参数 lzt
	public Map<String, Object> resultData = new HashMap<String, Object>();// 返回数据lzt
	public String msg;// 返回界面的提示信息lzt
	public int result;//lzt
	@Deprecated
	protected HttpServletRequest request = null;
	@Deprecated
	protected HttpServletResponse response = null;


	private int statusCode = 200;
	private String message = null;
	private String forwardUrl = null;

	private boolean skipVC = false;
	private String validationCode; // 验证�?

	// search form fields
	public final static int PAGE_SHOW_COUNT = 20;
	private int pageNum = 1;
	private int numPerPage = 0;
	private int totalPages;
	private String orderField;
	private String orderDirection;
	private String keywords;
	private int totalCount = 0;
	/**
	 * 根据名字获取cookie
	 * @param request
	 * @param name cookie名字
	 * @return
	 */
	public static Cookie getCookieByName(HttpServletRequest request,String name){
		Map<String,Cookie> cookieMap = ReadCookieMap(request);
		if(cookieMap.containsKey(name)){
			Cookie cookie = (Cookie)cookieMap.get(name);
			return cookie;
		}else{
			return null;
		}	
	}
	/**
	 * 将cookie封装到Map里面
	 * @param request
	 * @return
	 */
	private static Map<String,Cookie> ReadCookieMap(HttpServletRequest request){	
		Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
		Cookie[] cookies = request.getCookies();
		if(null!=cookies){
			for(Cookie cookie : cookies){
				cookieMap.put(cookie.getName(), cookie);
			}
		}
		return cookieMap;
	}
	/**
	 * 将cookie清除
	 * @param request
	 * @return
	 */
	public static void clearCookieMap(HttpServletRequest request,HttpServletResponse response){	
		Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
		Cookie[] cookies = request.getCookies();
		if(null!=cookies){
			for(Cookie cookie : cookies){
				Cookie cookie_temp = new Cookie(cookie.getName(), null);
				cookie_temp.setMaxAge(0);
				response.addCookie(cookie_temp);
			}
		}
	}
	/**
	 * 要批量操作的ID
	 */
	private String oprateIds;
	
	/**
	 * 要操作的单个ID
	 */
	private int oprateOndId = -1;
	//lzt
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	//lzt
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	public Map<String, String> getParms() {
		return parms;
	}
	public void setParms(Map<String, String> parms) {
		this.parms = parms;
	}
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	protected boolean uploadFile(File file, String filePath) {

		boolean retCode = false;
		byte[] buffer = new byte[1024];
		FileOutputStream fos = null;
		InputStream is = null;
		try {
			is = new FileInputStream(file);

			fos = new FileOutputStream(new File(filePath));

			int n = -1;
			while ((n = is.read(buffer, 0, buffer.length)) != -1) {
				fos.write(buffer, 0, n);
			}

			retCode = true;
			System.out.println("upload file success...");
		} catch (FileNotFoundException fnfe) {
			System.out.println("fnfe:" + fnfe);
		} catch (IOException ioe) {
			System.out.println("ioe:" + ioe);
		} finally {
			if (fos != null) {
				try {
					fos.close();
					fos = null;
				} catch (IOException e) {
					log.error(e);
				}
			}
			if (is != null) {
				try {
					is.close();
					is = null;
				} catch (IOException e) {
					log.error(e);
				}

			}
		}

		return retCode;
	}


	public int getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(int statusCode){
		this.statusCode = statusCode;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}	

	public boolean isSkipVC() {
		return skipVC;
	}

	public void setSkipVC(boolean skipVC) {
		this.skipVC = skipVC;
	}

	public String getValidationCode() {
		return validationCode;
	}

	public void setValidationCode(String validationCode) {
		this.validationCode = validationCode;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getNumPerPage() {
		return numPerPage > 0 ? numPerPage : PAGE_SHOW_COUNT;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public String realOrderField(){
		if ("desc".equalsIgnoreCase(orderDirection))
			return orderField+"_DESC";
		return orderField;
	}
	
	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	

	public String getOprateIds() {
		return oprateIds;
	}

	public void setOprateIds(String oprateIds) {
		this.oprateIds = oprateIds;
	}
	

	public int getOprateOndId() {
		return oprateOndId;
	}

	public void setOprateOndId(int oprateOndId) {
		this.oprateOndId = oprateOndId;
	}
	
	
}